<template>
  <div class="app-container">
    <el-dialog title="选择位置" v-if="showMap" :visible.sync="showMap" width="40%" :append-to-body="true" :close-on-click-modal="false" :close-on-press-escape="false" :destroy-on-close="true" top="5vh" >
      <iframe id="mapPage" style="width: 100%;height: 500px" frameborder=0
              src="https://apis.map.qq.com/tools/locpicker?search=1&type=1&key=RSQBZ-R2NER-KFMWS-W6OZN-UXMTE-H4BIF&referer=91885App">
      </iframe>
      <span slot="footer" class="dialog-footer">
            <el-button type="primary" class="primary"  @click="showMap = false">确 定</el-button>
          </span>
    </el-dialog>
    <el-form ref="placeCodeForm" v-loading="formLoading" :rules="rules" :model="form" label-position="top">
      <el-row type="flex" :gutter="20">
        <el-col :span="24">
          <el-form-item label="名称" prop="name" style="width: 400px">
            <el-input v-model="form.name" placeholder="请输入名称" clearable  />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="地址" prop="address" style="width: 400px">
            <div style="display: flex;">
              <el-input v-model="form.address" placeholder="请选择地址" disabled  />
              <el-button size="mini" style="margin-left: 10px" icon="el-icon-position" type="primary" @click="showMap=true">选择位置</el-button>
            </div>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex" :gutter="20">
        <el-col :span="24">
          <el-form-item label="经度" prop="longitude" style="width: 400px">
            <el-input v-model="form.longitude" placeholder="经度" disabled  />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="纬度" prop="latitude" style="width: 400px">
            <el-input v-model="form.latitude" placeholder="纬度" disabled  />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import {editIncubatorsApi, getBsIncubatorsByIdApi} from "@/api/entrepreneurship";
import axios from "axios";
export default {
  components: { Editor, Toolbar },
  name: 'PlaceCodeDetail',
  data(){
    return{
      editor: null,
      showMap:false,
      liveTime:[],
      comList:{},
      userList:{},
      publishList:{},
      toolbarConfig: {
        excludeKeys: [
          // 排除菜单组，写菜单组 key 的值即可
          'group-video' // 去掉视频
        ]
      },
      editorConfig: { MENU_CONF: {}, placeholder: '' },
      mode: 'default',
      uploadUrl: process.env.VUE_APP_UPLOAD_API,
      dialogImageUrl: '',
      dialogVisible: false,
      form: {
        id: null,
        name:'',
        address:'',
        longitude: '',
        latitude: '',
        baseId: 0,

      },

      rules: {
        name: [{ required: true, trigger: 'blur', message: '请输入名称' }],
        address: [{ required: true, trigger: 'blur', message: '请填写名称' }],
        longitude:[{ required: true, trigger: 'blur', message: '请填写经度' }],
        latitude:[{ required: true, trigger: 'blur', message: '请填写纬度' }],
      },
      formLoading: false,
    }
  },
  props:['id','baseId','closeDialog'],
  methods:{
    handleAvatarSuccess(res, file) {
      this.form.logo = res.data;
    },
    onCreated(editor) {
      this.editor = Object.seal(editor) // 一定要用 Object.seal() ，否则会报错
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isLt2M;
    },
    submit(){
      this.$refs.placeCodeForm.validate(valid => {
        if (valid) {
          const loading=this.$loading({
            text:'提交中'
          })
          if(this.form.id==null)
          {
            this.form.id = 0;
          }
          const form=JSON.parse(JSON.stringify(this.form));
          form.baseId = this.baseId
          editIncubatorsApi(form).then((res)=>{
            this.$message.success('保存成功');
            this.closeDialog(0);
          }).catch(()=>{
            this.$message.error('保存失败');
          }).finally(()=>{
            loading.close();
          })
        } else {
          return false
        }
      })
    },
    getDetail(){
      getBsIncubatorsByIdApi({id:this.id}).then((res)=>{
        this.form=res;
      }).finally(()=>{
        this.formLoading=false;
      })
    },
  },
  watch: {

  },
  mounted() {
    window.addEventListener('message', (event) =>{
      // 接收位置信息，用户选择确认位置点后选点组件会触发该事件，回传用户的位置信息
      let loc = event.data;
      if (loc && loc.module == 'locationPicker') {//防止其他应用也会向该页面post信息，需判断module是否为'locationPicker'
        this.form.latitude=loc.latlng.lat;
        this.form.longitude=loc.latlng.lng;
        this.form.address=loc.poiname
        axios.get(`https://apis.map.qq.com/ws/geocoder/v1/?location=${loc.latlng.lat},${loc.latlng.lng}&key=RSQBZ-R2NER-KFMWS-W6OZN-UXMTE-H4BIF`).then((res)=>{
          console.log(res);
        })
      }
    }, false);
    if(this.id){
      this.getDetail();
    }

  },

}
</script>
<style src="@wangeditor/editor/dist/css/style.css"></style>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #c5c5c5;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;

}
.w-e-bar.w-e-bar-show.w-e-toolbar{
  border: 1px solid;
}
.w-e-scroll{
  border: 1px solid;
}
</style>
<style scoped>
/*
*让select撑开
*/
.el-select,.el-cascader{
  display: block;
}
.avatar-uploader .el-upload {
  border: 1px dashed #c5c5c5;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;

}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
